﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using BUS;
using DTO;

namespace QuanLyHocSinh.GUI
{
    public partial class F_QuanLyDiem : DevExpress.XtraEditors.XtraUserControl
    {
        public F_QuanLyDiem()
        {
            InitializeComponent();
        }
        double check = 0.0;
        int trangthaiform = 0;

        private void GroupTimKiemThongTinHocSinhEnableFalse()
        {
            this.lkHocKy.Enabled = false;
            this.lkTenLop.Enabled = false;
            this.lkMonHoc.Enabled = false;
            this.lkKhoiLop.Enabled = false;
            this.btnThucHien.Enabled = false;
        }

        private void GroupTimKiemThongTinHocSinhEnableTrue()
        {
            this.lkKhoiLop.Enabled = true;
            this.lkTenLop.Enabled = true;
            this.lkMonHoc.Enabled = true;
            this.lkHocKy.Enabled = true;

            this.lkKhoiLop.EditValue = null;
            this.lkTenLop.EditValue = null;
            this.lkMonHoc.EditValue = null;
            this.lkHocKy.EditValue = null;

            this.btnThucHien.Enabled = true;
        }

        private void GroupThongTinHocSinhEnableFalse()
        {
            this.lbTenHocSinh.Text = "NONE";
            this.lbMonHoc.Text = "NONE";
            this.lbHocKy.Text = "NONE";
            this.lbLop.Text = "NONE";

            this.txtDiem15.Enabled = false;
            this.txtDiem1Tiet.Enabled = false;
            this.txtDiemCuoiKy.Enabled = false;
            this.btnCapNhat.Enabled = false;
            this.btnHoanTat.Enabled = false;

            this.txtDiem15.Text = this.txtDiem1Tiet.Text = this.txtDiemCuoiKy.Text = this.txtDiemTB.Text = "";
        }

        private void GroupThongTinHocSinhEnableTrue()
        {
            this.btnHoanTat.Enabled = true;
        }
        private void btnThucHien_Click(object sender, EventArgs e)
        {
            try
            {
                if (lkKhoiLop.EditValue != null)
                {
                    if (lkTenLop.EditValue != null)
                    {
                        if (lkMonHoc.EditValue != null)
                        {
                            if (lkHocKy.EditValue != null)
                            {
                                DataTable dulieu = null;
                                DTODiem diem = new DTODiem();
                                diem.MaMH = lkMonHoc.EditValue.ToString();
                                diem.MaHK = lkHocKy.EditValue.ToString();
                                dulieu = BUSQuanLyDiem.BangDiemHocSinh(lkTenLop.EditValue, diem);
                                if (dulieu.Rows.Count > 0)
                                {
                                    dgvDiem.DataSource = dulieu;
                                    GroupTimKiemThongTinHocSinhEnableFalse();
                                    GroupThongTinHocSinhEnableTrue();
                                }
                                else
                                {
 
                                }
                            }
                            else
                            {
                                XtraMessageBox.Show("Vui Lòng Chọn Học Kỳ!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            XtraMessageBox.Show("Vui Lòng Chọn Môn Học!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("Vui Lòng Chọn Lớp!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    XtraMessageBox.Show("Vui Lòng Chọn Khối Lớp!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void F_QuanLyDiem_Load(object sender, EventArgs e)
        {
            LoadLookUpEdit();       
        }

        private void LoadLookUpEdit()
        {
            try
            {
                this.lkTenLop.EditValue = null;
                this.lkMonHoc.EditValue = null;
                DataTable dulieulookup = new DataTable();

                dulieulookup = BUSQuanLyDiem.LietKeKhoiLop();
                this.lkKhoiLop.Properties.DataSource = dulieulookup;
                this.lkKhoiLop.Properties.DisplayMember = "TENKHOILOP";
                this.lkKhoiLop.Properties.ValueMember = "MAKHOILOP";
                this.lkKhoiLop.Properties.ShowHeader = false;
                this.lkKhoiLop.Properties.ShowFooter = false;
                this.lkKhoiLop.Properties.ImmediatePopup = false;
                this.lkKhoiLop.Properties.SearchMode = SearchMode.AutoComplete;

                dulieulookup = BUSQuanLyDiem.LietKeHocKy();
                this.lkHocKy.Properties.DataSource = dulieulookup;
                this.lkHocKy.Properties.DisplayMember = "TENHK";
                this.lkHocKy.Properties.ValueMember = "MAHK";
                this.lkHocKy.Properties.ShowHeader = false;
                this.lkHocKy.Properties.ShowFooter = false;
                this.lkHocKy.Properties.ImmediatePopup = false;
                this.lkHocKy.Properties.SearchMode = SearchMode.AutoComplete;
            }
            catch (Exception ex)
            {
 
            }
        }

        private void lkKhoiLop_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dulieulookup = new DataTable();

                dulieulookup = BUSQuanLyDiem.LietKeLop(lkKhoiLop.EditValue);
                this.lkTenLop.Properties.DataSource = dulieulookup;
                this.lkTenLop.Properties.DisplayMember = "TENLOP";
                this.lkTenLop.Properties.ValueMember = "MALOP";
                this.lkTenLop.Properties.ShowHeader = false;
                this.lkTenLop.Properties.ShowFooter = false;
                this.lkTenLop.Properties.ImmediatePopup = false;
                this.lkTenLop.Properties.SearchMode = SearchMode.AutoComplete;

                dulieulookup = BUSQuanLyDiem.LietKeMonHoc(lkKhoiLop.EditValue);
                this.lkMonHoc.Properties.DataSource = dulieulookup;
                this.lkMonHoc.Properties.DisplayMember = "TENMONHOC";
                this.lkMonHoc.Properties.ValueMember = "MAMONHOC";
                this.lkMonHoc.Properties.ShowHeader = false;
                this.lkMonHoc.Properties.ShowFooter = false;
                this.lkMonHoc.Properties.ImmediatePopup = false;
                this.lkMonHoc.Properties.SearchMode = SearchMode.AutoComplete;
            }
            catch (Exception ex)
            {
 
            }
        }

        private void gvDiem_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                if (trangthaiform == 0)
                {
                    this.btnCapNhat.Enabled = true;
                    this.lbMaHS.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["MAHS"]).ToString().Trim();
                    this.lbTenHocSinh.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["HOTEN"]).ToString().Trim();
                    this.lbMonHoc.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["TENMONHOC"]).ToString().Trim();
                    this.lbHocKy.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["TENHK"]).ToString().Trim();
                    this.lbLop.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["TENLOP"]).ToString().Trim();
                    this.txtDiem15.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["DIEM15P"]).ToString().Trim();
                    this.txtDiem1Tiet.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["DIEM1T"]).ToString().Trim();
                    this.txtDiemCuoiKy.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["DIEMTHI"]).ToString().Trim();
                    this.txtDiemTB.Text = gvDiem.GetRowCellValue(e.RowHandle, gvDiem.Columns["DIEMTB"]).ToString().Trim();
                }
                else
                {
                    XtraMessageBox.Show("Vui lòng hoàn tất cập nhật Điểm!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnHoanTat_Click(object sender, EventArgs e)
        {
            if (this.btnHoanTat.Text == "Hoàn Tất")
            {
                GroupTimKiemThongTinHocSinhEnableTrue();
                GroupThongTinHocSinhEnableFalse();
                dgvDiem.DataSource = null;
            }
            else
            {
                trangthaiform = 0;
                this.btnHoanTat.Text = "Hoàn Tất";
                this.btnCapNhat.Text = "Cập Nhật";
                this.txtDiem15.Enabled = this.txtDiem1Tiet.Enabled = this.txtDiemCuoiKy.Enabled = this.txtDiemTB.Enabled = false;
                this.txtDiem15.Text = this.txtDiem1Tiet.Text = this.txtDiemCuoiKy.Text = this.txtDiemTB.Text = "";
                this.lbTenHocSinh.Text = "NONE";
                this.lbMonHoc.Text = "NONE";
                this.lbHocKy.Text = "NONE";
                this.lbLop.Text = "NONE";
            }
        }

        private void txtDiemTB_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtDiem15_KeyPress(object sender, KeyPressEventArgs e)
        {
            GlobalFunction.KiemTraChieuDaiChuoi(this.txtDiem15.Text, 2, e);
        }

        private void txtDiem1Tiet_KeyPress(object sender, KeyPressEventArgs e)
        {
            GlobalFunction.KiemTraChieuDaiChuoi(this.txtDiem1Tiet.Text, 2, e);
        }

        private void txtDiemCuoiKy_KeyPress(object sender, KeyPressEventArgs e)
        {
            GlobalFunction.KiemTraChieuDaiChuoi(this.txtDiemCuoiKy.Text, 2, e);
        }

        private void txtDiem15_Leave(object sender, EventArgs e)
        {
            if (double.TryParse(txtDiem15.Text, out check) == false || this.txtDiem15.Text == "" || double.Parse(txtDiem15.Text) < 0 || double.Parse(txtDiem15.Text) >10)
            {
                this.txtDiem15.Focus();
                XtraMessageBox.Show("Điểm 15 PHÚT nhập vào hệ thống không hợp lệ! Kiểm tra lại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                double diemtrungbinh = 0;
                diemtrungbinh = Math.Round((double.Parse(txtDiem15.Text.Replace(",", ".")) + double.Parse(txtDiem1Tiet.Text) * 2 + double.Parse(txtDiemCuoiKy.Text) * 3) / 6, 1);
                this.txtDiemTB.Text = diemtrungbinh.ToString();
            }
        }

        private void txtDiem1Tiet_Leave(object sender, EventArgs e)
        {
            if (double.TryParse(txtDiem1Tiet.Text, out check) == false || this.txtDiem1Tiet.Text == "" || double.Parse(txtDiem1Tiet.Text) < 0 || double.Parse(txtDiem1Tiet.Text) > 10)
            {
                this.txtDiem1Tiet.Focus();
                XtraMessageBox.Show("Điểm 1 TIẾT nhập vào hệ thống không hợp lệ! Kiểm tra lại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                double diemtrungbinh = 0;
                diemtrungbinh = Math.Round((double.Parse(txtDiem15.Text) + double.Parse(txtDiem1Tiet.Text.Replace(",", ".")) * 2 + double.Parse(txtDiemCuoiKy.Text) * 3) / 6, 1);
                this.txtDiemTB.Text = diemtrungbinh.ToString();
            }
        }

        private void txtDiemCuoiKy_Leave(object sender, EventArgs e)
        {
            if (double.TryParse(txtDiemCuoiKy.Text, out check) == false || this.txtDiemCuoiKy.Text == "" || double.Parse(txtDiemCuoiKy.Text) < 0 || double.Parse(txtDiemCuoiKy.Text) > 10)
            {
                this.txtDiemCuoiKy.Focus();
                XtraMessageBox.Show("Điểm CUỐI KỲ nhập vào hệ thống không hợp lệ! Kiểm tra lại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                double diemtrungbinh = 0;
                diemtrungbinh = Math.Round((double.Parse(txtDiem15.Text) + double.Parse(txtDiem1Tiet.Text) * 2 + double.Parse(txtDiemCuoiKy.Text.Replace(",", ".")) * 3) / 6, 1);
                this.txtDiemTB.Text = diemtrungbinh.ToString();
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (this.btnCapNhat.Text == "Cập Nhật")
            {
                this.btnCapNhat.Text = "Lưu";
                this.btnHoanTat.Text = "Bỏ Qua";
                trangthaiform = 1;
                this.txtDiem15.Enabled = this.txtDiem1Tiet.Enabled = this.txtDiemCuoiKy.Enabled = this.txtDiemTB.Enabled = true;
            }
            else
            {
                this.btnHoanTat.Text = "Hoàn Tất";
                this.btnCapNhat.Text = "Cập Nhật";
                trangthaiform = 0;
                DTODiem diem = new DTODiem();
                diem.MaHS = lbMaHS.Text;
                diem.MaHK = lkHocKy.EditValue.ToString();
                diem.MaMH = lkMonHoc.EditValue.ToString();
                diem.Diem15P = double.Parse(this.txtDiem15.Text);
                diem.Diem1T = double.Parse(this.txtDiem1Tiet.Text);
                diem.DiemThi = double.Parse(this.txtDiemCuoiKy.Text);
                diem.DiemTB = double.Parse(this.txtDiemTB.Text);
                int ketqua = 0;
                ketqua = BUSQuanLyDiem.CapNhatDiemHocSinh(diem);
                this.txtDiem15.Enabled = false;
                this.txtDiem1Tiet.Enabled = false;
                this.txtDiemCuoiKy.Enabled = false;
                this.txtDiemTB.Enabled = false;
                this.dgvDiem.DataSource = BUSQuanLyDiem.BangDiemHocSinh(lkTenLop.EditValue, diem);
            }
        }
    }
}
